package com.rivulet.foreign.service.impl;

import com.rivulet.foreign.mapper.UserMapper;
import com.rivulet.foreign.service.ToolService;
import com.rivulet.foreign.tools.Lock;
import org.springframework.stereotype.Service;

import static com.rivulet.foreign.tools.Identity.BIG_TEACHER;
import static com.rivulet.foreign.tools.Identity.TEACHER;

/**
 * {自定义验证功能}
 *
 * @author xujiac
 * @create 2021/7/27
 * @since 1.0.0
 */
@Service
public class ToolServiceImpl implements ToolService {
    final UserMapper mapper;

    public ToolServiceImpl(UserMapper mapper) {
        this.mapper = mapper;
    }

    public Integer getIdentity(String user_id) {
        Integer result = 0;
        try {
            String id = Lock.lockOff(user_id);
            result = mapper.getIdentityById(id);
        } catch (Exception e) {
            result = -1;
        }
        return result;
    }

    @Override
    public boolean reAuthorization(String user_id, Integer identity) {
        if (getIdentity(user_id).equals(identity)) {
            return true;
        } else if (identity >= TEACHER && identity <= BIG_TEACHER) {
            if ((getIdentity(user_id) >= TEACHER) && (getIdentity(user_id) <= BIG_TEACHER)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}
